﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class GenerateHelper
    {
        /// <summary>
        /// 生成随机码
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetRandomNum(int length)
        {
            string BASECODE = "0123456789";
            Random ranNum = new Random((int)DateTime.Now.Ticks);

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                int rnNum = ranNum.Next(BASECODE.Length);
                builder.Append(BASECODE[rnNum]);
            }
            return builder.ToString();
        }

        /// <summary>
        /// 参数按照ASCII码从小到大排序
        /// </summary>
        /// <param name="paramsMap"></param>
        /// <returns></returns>
        public static string SortParamSrc(Dictionary<string, string> paramsMap)
        {
            var vDic = (from objDic in paramsMap orderby objDic.Key ascending select objDic);
            StringBuilder str = new StringBuilder();
            foreach (KeyValuePair<string, string> kv in vDic)
            {
                str.Append(kv.Key + "=" + kv.Value + "&");
            }
            return str.ToString().Substring(0, str.ToString().Length - 1);
        }
    }
}
